package com.ruoyi.wq.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wq.domain.WqDynamicTopic;
import com.ruoyi.wq.domain.WqTopic;
import com.ruoyi.wq.domain.WqTopicUser;
import com.ruoyi.wq.domain.vo.WqTopicVO;
import com.ruoyi.wq.mapper.WqTopicMapper;
import com.ruoyi.wq.service.IWqTopicService;
import com.ruoyi.wq.service.IWqTopicUserService;

/**
 * 话题Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-17
 */
@Service
public class WqTopicServiceImpl extends ServiceImpl<WqTopicMapper, WqTopic> implements IWqTopicService 
{
    @Autowired
    private WqTopicMapper wqTopicMapper;
    
    @Autowired
    private IWqTopicUserService wqTopicUserService;

    /**
     * 查询话题
     * 
     * @param id 话题主键
     * @return 话题
     */
    @Override
    public WqTopic selectWqTopicById(String id)
    {
        return wqTopicMapper.selectWqTopicById(id);
    }

    /**
     * 查询话题列表
     * 
     * @param wqTopic 话题
     * @return 话题
     */
    @Override
    public List<WqTopic> selectWqTopicList(WqTopic wqTopic)
    {
        return wqTopicMapper.selectWqTopicList(wqTopic);
    }

    /**
     * 新增话题
     * 
     * @param wqTopic 话题
     * @return 结果
     */
    @Override
    public int insertWqTopic(WqTopic wqTopic)
    {
        wqTopic.setCreateTime(DateUtils.getNowDate());
        return wqTopicMapper.insertWqTopic(wqTopic);
    }

    /**
     * 修改话题
     * 
     * @param wqTopic 话题
     * @return 结果
     */
    @Override
    public int updateWqTopic(WqTopic wqTopic)
    {
        wqTopic.setUpdateTime(DateUtils.getNowDate());
        return wqTopicMapper.updateWqTopic(wqTopic);
    }

    /**
     * 批量删除话题
     * 
     * @param ids 需要删除的话题主键
     * @return 结果
     */
    @Override
    public int deleteWqTopicByIds(String[] ids)
    {
        return wqTopicMapper.deleteWqTopicByIds(ids);
    }

    /**
     * 删除话题信息
     * 
     * @param id 话题主键
     * @return 结果
     */
    @Override
    public int deleteWqTopicById(String id)
    {
        return wqTopicMapper.deleteWqTopicById(id);
    }

	@Override
	public List<WqTopic> getHotTopicList(Boolean flag) {
		QueryWrapper<WqTopic> qw = new QueryWrapper<>();
		WqTopic topic = new WqTopic();
		if(flag){
			topic.setId("feb7f98dd8b265d3d6ffad4767541369");
		}
		topic.setHot(true);
		qw.setEntity(topic);
		qw.orderByDesc("sort");
		qw.orderByDesc("give_num");
		return baseMapper.selectList(qw);
	}

	@Override
	public List<WqTopic> getTopicList() {
		QueryWrapper<WqTopic> qw = new QueryWrapper<>();
		qw.orderByDesc("sort");
		qw.orderByDesc("give_num");
		return baseMapper.selectList(qw);
	}

	@Override
	public Boolean fans(String id) {
		WqTopicUser topicUser = wqTopicUserService.getTopicUserByTopicId(id);
		WqTopic topic = baseMapper.selectById(id);
		if(topicUser == null){
			// 关注
			topicUser = new WqTopicUser();
			topicUser.setTopicId(topic.getId());
			topicUser.setImage(topic.getImage());
			topicUser.setTitle(topic.getTitle());
			topicUser.setTopicDesc(topic.getTopicDesc());
			topicUser.setUserId(SecurityUtils.getUserId());
			wqTopicUserService.save(topicUser);
			
			topic.setFansNum(topic.getFansNum()+1);
			baseMapper.updateById(topic);
			return true;
		}else{
			// 取消关注
			wqTopicUserService.removeById(topicUser.getId());
			
			topic.setFansNum(topic.getFansNum()-1);
			baseMapper.updateById(topic);
			return false;
		}
	}

	@Override
	public WqTopicVO getDetail(String id) {
		WqTopicUser wqTopicUser = wqTopicUserService.getTopicUserByTopicId(id);
		WqTopic wqTopic = baseMapper.selectById(id);
		WqTopicVO topicVO = new WqTopicVO();
		BeanUtils.copyProperties(wqTopic, topicVO);
		if(wqTopicUser == null){
			topicVO.setFans(false);
		}else{
			topicVO.setFans(true);
		}
		return topicVO;
	}

	@Override
	public List<WqTopicVO> getList() {
		List<WqTopic> topicList = baseMapper.selectList(null);
		List<WqTopicVO> wqTopicVOList = new ArrayList<>();
		for(WqTopic wqTopic : topicList){
			WqTopicUser wqTopicUser = wqTopicUserService.getTopicUserByTopicId(wqTopic.getId());
			if(wqTopicUser == null){
				WqTopicVO wqTopicVO = new WqTopicVO();
				BeanUtils.copyProperties(wqTopic, wqTopicVO);
				wqTopicVO.setFans(false);
				wqTopicVOList.add(wqTopicVO);
			}
		}
		
		List<WqTopicVO> topicVOList = new ArrayList<WqTopicVO>();
		WqTopicVO topicVO = new WqTopicVO();
		for(int i=0;i<wqTopicVOList.size();i++){
			WqTopicVO topic = wqTopicVOList.get(i);
			topicVO.getTopicList().add(topic);
			
			if(topicVO.getTopicList().size() == 3){
				topicVOList.add(topicVO);
				topicVO = new WqTopicVO();
				continue;
			}
			
			if(i == wqTopicVOList.size()-1){
				topicVOList.add(topicVO);
			}
		}
		return topicVOList;
	}

	@Override
	public Map<String, WqDynamicTopic> getTopicMap() {
		Map<String,WqDynamicTopic> map = new HashMap<String, WqDynamicTopic>();
		List<WqTopic> topicList = baseMapper.selectList(null);
		for(WqTopic topic : topicList){
			WqDynamicTopic dt = new WqDynamicTopic();
			dt.setTopicId(topic.getId());
			dt.setTitle(topic.getTitle());
			map.put(topic.getId(), dt);
		}
		return map;
	}

}
